package com.example.color_test.utils;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

@Data
public class FilePath {

    static String baseName = "color_test_images";

    /**
     * 图片存储路径
     */
   static String imgPath = System.getProperty("user.dir")+"/../"+baseName+"/";
   static String _selfPath = System.getProperty("user.dir")+"/../";

   static public String getAllImgPath(String path){
       return _selfPath + path;
   }
    //上传图片
    public static String uploadFile(MultipartFile uploadFile) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        String res = sdf.format(new Date());
        //设置的保存路径
        String rootPath = imgPath;
        //原始名称
        String originalFilename = uploadFile.getOriginalFilename();
        //生成文件名
        String newFileName = res + UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //创建年月日时分文件夹 ==》 此处按天存
        Calendar date = Calendar.getInstance();
//        File dateDirs = new File(date.get(Calendar.YEAR)
//                + File.separator + (date.get(Calendar.MONTH) + 1)
//                + File.separator + (date.get(Calendar.DATE))
//                + File.separator + (date.get(Calendar.HOUR_OF_DAY))
//                + File.separator + (date.get(Calendar.MINUTE)));
//        File dateDirs = new File(date.get(Calendar.YEAR)
//                + File.separator + (date.get(Calendar.MONTH) + 1)
//                + File.separator + (date.get(Calendar.DATE))
//                + File.separator + (date.get(Calendar.HOUR_OF_DAY)));
        File dateDirs = new File(date.get(Calendar.YEAR)
                + File.separator + (date.get(Calendar.MONTH) + 1)
                + File.separator + (date.get(Calendar.DATE)));
        System.out.println("图片存储地址："+rootPath + File.separator + dateDirs + File.separator + newFileName);
        //新文件
        File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
        //判断目标文件所在的目录是否存在
        if (!newFile.getParentFile().exists()) {
            //如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }

        //将内存中的数据写入磁盘
        uploadFile.transferTo(newFile);
        //完整的url
        String fileUrl = baseName + "/" +date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH) + 1) + "/" + (date.get(Calendar.DATE)) + "/" + newFileName;
        return fileUrl;
    }
    /**
     * 判断上传文件大小方法
     *
     * @param len
     *            文件长度
     * @param size
     *            限制大小
     * @return
     */
    public static boolean checkFileSize(Long len, int size) {
        double fileSize = 0;
        fileSize = (double) len / 1048576;
        if (fileSize > size) {
            return false;
        }
        return true;
    }

}
